package com.application.fabric.fabriccontrol.existevidence;

import com.application.fabric.fabriccontrol.config.PDFmsg;
import com.application.fabric.users.config.Dealmsg;
import com.application.fabric.users.config.HDFS.HadoopTemplate;
import com.application.fabric.users.entity.ExtractToken;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.FileOutputStream;

@Repository
public class CreatePDF {
    @Autowired
    private PDFmsg pdFmsg;


    //    创建用户目录
    @Resource
    public ExtractToken token;
    @Autowired
    private Dealmsg dealmsg;
    @Autowired
    private HadoopTemplate hadoopTemplate;

    public void  writehdfs() throws Exception {
        main();
        hadoopTemplate.uploadFile(pdFmsg.getPfdpath(),pdFmsg.getHDFSpath());
    }

    public void main() throws Exception{
        Document document = new Document();
       PdfWriter.getInstance(document, new FileOutputStream("src/main/java/com/application/fabric/fabriccontrol/existevidence/capool/"+pdFmsg.getPdfname()+".pdf"));
//       hdfs创建
       pdFmsg.setPfdpath("src/main/java/com/application/fabric/fabriccontrol/existevidence/capool/"+pdFmsg.getPdfname());
       pdFmsg.setHDFSpath("/certificate/"+"appUser"+pdFmsg.getPdfname());
        hadoopTemplate.existDir(pdFmsg.getPfdpath(),true);
        document.open();
        Paragraph p1 = new Paragraph("Fabric  Voucher");
        /*居中对齐*/
        p1.setAlignment(Element.ALIGN_CENTER);
        setStyle(p1,50,30,10,10,10,0);
        document.add(p1);


        Paragraph p12 = new Paragraph("--------------------------------------------------");
        /*居中对齐*/
        p12.setAlignment(Element.ALIGN_CENTER);
        setStyle(p12,50,30,10,10,10,0);
        document.add(p12);

        Paragraph p2 = new Paragraph("Certificate:"+pdFmsg.getCertificate());
        setStyle(p2,30,30,10,10,10,100);
        document.add(p2);

        Paragraph p3 = new Paragraph("Hash ID:"+pdFmsg.getHashid());
        setStyle(p3,30,30,10,10,10,100);
        document.add(p3);


        Paragraph p4 = new Paragraph("Block address:"+pdFmsg.getBlockaddress());

        setStyle(p4,30,30,10,10,10,100);
        document.add(p4);

        Paragraph p5 = new Paragraph("File name:"+pdFmsg.getPdfname());
        setStyle(p5,30,30,10,10,10,100);
        document.add(p5);

        Paragraph p6 = new Paragraph("Time:"+pdFmsg.getTime());
        setStyle(p6,30,30,10,10,10,100);
        document.add(p6);


        Paragraph p7 = new Paragraph("Issuing authority:"+"fabric ca");
        setStyle(p7,30,30,10,10,10,100);
        document.add(p7);


        Paragraph p13 = new Paragraph("--------------------------------------------------");
        /*居中对齐*/
        p13.setAlignment(Element.ALIGN_CENTER);
        setStyle(p13,50,30,10,10,10,0);
        document.add(p12);


        Paragraph p8 = new Paragraph("Explain:");
        setStyle(p8,30,40,10,10,10,150);
        document.add(p8);


        Paragraph p9 = new Paragraph(" This file is encrypted by sm3");
        setStyle(p9,30,20,10,10,10,160);
        document.add(p9);



        Paragraph p11 = new Paragraph("Data cannot be tampered with through fabric storage");
        setStyle(p11,30,20,10,10,10,160);
        document.add(p11);



        document.close();
    }
    private static Paragraph setStyle(Paragraph p,int setPaddingTop,int setSize,int setColor1,int setColor2,int setColor3,int left){
        p.setPaddingTop(setPaddingTop);
//      Font font = new Font();
        Font font = FontFactory.getFont(FontFactory.HELVETICA, 4f);
        font.setSize(setSize);
        font.setColor(setColor1,setColor2,setColor3);
        p.setFont(font);
        p.setIndentationLeft(left);
        return p;
    }
}
